<?php

namespace addons\shopro\controller;


use addons\shopro\model\User;
use think\Db;

class Order extends Base
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function index()
    {
        $params = $this->request->get();

        $this->success('订单列表', \addons\shopro\model\Order::getList($params));
    }



    public function detail()
    {
        $params = $this->request->get();
        $this->success('订单详情', \addons\shopro\model\Order::detail($params));
    }


    public function itemDetail()
    {
        $params = $this->request->get();
        $this->success('订单商品', \addons\shopro\model\Order::itemDetail($params));
    }


    // 即将废弃
    public function statusNum()
    {
        $this->success('订单数量', \addons\shopro\model\Order::statusNum());
    }


    // 取消订单
    public function cancel()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'cancel');

        $this->success('取消成功', \addons\shopro\model\Order::operCancel($params));
    }

    // 删除订单
    public function delete()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'delete');

        $this->success('删除成功', \addons\shopro\model\Order::operDelete($params));
    }

    // 确认收货
    public function confirm_XXX()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'confirm');

        $this->success('收货成功', \addons\shopro\model\Order::operConfirm($params));
    }

    // 确认收货
    public function confirm()
    {
        $params = $this->request->post();

        // 表单验证
//        $this->shoproValidate($params, get_class(), 'confirm');


        $item_list=Db::name('shopro_order_item')
            ->where('order_id',$params['id'])
            ->field('id')
            ->select();

        if(empty($item_list)){
            $this->error('收货失败');
        }
        $r='';
        foreach ($item_list as $v){
            $params2=array();
            $params2['id']=$params['id'];
            $params2['order_item_id']=$v['id'];
            $r=\addons\shopro\model\Order::operConfirm($params2);
        }

        $user = User::info();
        //收货后赠送积分
        $shopro_order=Db::name('shopro_order')->where('id',$params['id'])->find();
        if($shopro_order['newadd_score']>0) {
            $time = time();
            \addons\shopro\model\User::score($shopro_order['newadd_score'], $user->id, 'newadd_score', $params['id'], '', [
                'date' => date('Y-m-d', $time)
            ]);
        }

        // TODO 用户升级不生效
        //判断用户升级
        if($user->level!=5){
            $user_pay_price_sum=Db::name('shopro_order_item')
                ->where('user_id',$user->id)
                ->where('dispatch_status',2)
                ->where('aftersale_status!=2')
                ->where('refund_status!=2')
                ->where('deletetime',null)
                ->sum('pay_price');
            $user_up_lv=Db::name('user_level')
                ->where('lv','>',$user->level)
                ->where('up_money','<=',$user_pay_price_sum)
                ->order('lv desc')
                ->find();
            if(!empty($user_up_lv)){
                $insData=array();
                $insData['level']=$user_up_lv['lv'];
                Db::name('user')->where('id',$user->id)->update($insData);
            }
        }

        $this->success('收货成功', $r);
    }

    public function comment()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'comment');

        $this->success('评价成功', \addons\shopro\model\Order::operComment($params));
    }


    public function pre()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'pre');

        $result = \addons\shopro\model\Order::pre($params);

        if (isset($result['msg']) && $result['msg']) {
            $this->error($result['msg'], $result);
        } else {
            $this->success('计算成功', $result);
        }
    }


    public function createOrder()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'createOrder');

        $order = \addons\shopro\model\Order::createOrder($params);

        if(is_string($order))$this->error($order);

        $this->success('订单添加成功', $order);
    }


    // 获取可用优惠券列表
    public function coupons()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'coupons');

        $coupons = \addons\shopro\model\Order::coupons($params);

        $this->success('获取成功', $coupons);
    }
}
